﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ShenLanCMS.Models;

namespace ShenLanCMS.Web.Controllers
{
    public class AdController : Controller
    {
        private EntityContext db = new EntityContext();

        [IsAdmin]
        public ViewResult Index()
        {
            return View(db.Ads.ToList());
        }

        //广告js
        public ActionResult JS(Guid id)
        {
            try
            {
                var ad = db.Ads.Find(id);
                //判断广告存在或已经存在启用
                if (ad == null || ad.IsEnable == false)
                    return JavaScript("广告不存在或未启用");
                var strHtml = new System.Text.StringBuilder();
                //strHtml.Append(ad.Content);
                if (!string.IsNullOrEmpty(ad.Content))
                    ad.Content = ad.Content.Replace("\"", "'");
                strHtml.Append("document.write('" + ad.Content + "');");
                return JavaScript(strHtml.ToString());
            }
            catch { return JavaScript("获取广告失败"); }
        }

        [IsAdmin]
        public ViewResult Details(Guid id)
        {
            Ad ad = db.Ads.Find(id);
            return View(ad);
        }

        [IsAdmin]
        [ValidateInput(false)]
        public ActionResult Create()
        {
            ViewData["AdTypeID"] = new SelectList(db.AdTypes, "AdTypeID", "TypeName");
            return View();
        }

        [IsAdmin]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Ad ad)
        {
            ViewData["AdTypeID"] = new SelectList(db.AdTypes, "AdTypeID", "TypeName");
            if (ModelState.IsValid)
            {
                ad.AdID = Guid.NewGuid();
                db.Ads.Add(ad);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(ad);
        }

        [IsAdmin]
        [ValidateInput(false)]
        public ActionResult Edit(Guid id)
        {
            Ad ad = db.Ads.Find(id);
            ViewData["AdTypeID"] = new SelectList(db.AdTypes, "AdTypeID", "TypeName", ad.AdTypeID);
            return View(ad);
        }

        [IsAdmin]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Ad ad)
        {

            if (ModelState.IsValid)
            {
                db.Entry(ad).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(ad);
        }

        [IsAdmin]
        public ActionResult Delete(Guid id)
        {
            try
            {
                Ad ad = db.Ads.Find(id);
                db.Ads.Remove(ad);
                db.SaveChanges();
                return Json("删除成功");
            }
            catch { return Json("删除失败"); }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}